<template>
    <el-tabs type="card" v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="算法版本" name="first">
            <el-card class="box-card">
                <el-row>
                    <el-col :span="4">算法模块:</el-col>
                    <el-col :span="20"><el-tag>{{ serverStatus }}</el-tag> 
                        <el-button style="margin-left: 10px;" :loading="buttonLoading" type="primary" size="mini" @click="submitRestartAlgorithm">重 启</el-button></el-col>
                </el-row>
                <el-divider></el-divider>
                <el-row>
                    <el-col :span="4">算法授权:</el-col>
                    <el-col :span="20"><el-tag>正常</el-tag></el-col>
                </el-row>
                <el-divider></el-divider>
                <el-row>
                    <el-col :span="4">算法版本:</el-col>
                    <el-col :span="20"><el-tag>{{ algorithmVersion }}</el-tag></el-col>
                </el-row>
                <el-divider></el-divider>
                <el-row>
                    <el-col :span="4">模块版本:</el-col>
                    <el-col :span="20"><el-tag>{{ serviceVersion }}</el-tag></el-col>
                </el-row>
            </el-card>
        </el-tab-pane>
        <el-tab-pane label="全局配置" name="second">
            <el-card class="box-card">
                <el-form ref="globalConfig" :model="globalConfig" label-width="140px">

                    <el-form-item label="事件上报地址" prop="upload_address">
                        <el-input v-model="globalConfig.upload_address" placeholder="数据上报地址" />
                    </el-form-item>
                    <el-form-item label="图片存储目录" prop="storage_dir">
                        <el-input v-model="globalConfig.storage_dir" placeholder="图片存储目录" />
                    </el-form-item>
                    <el-form-item label="是否上传扣图" prop="crop_image">
                        <el-radio-group v-model="globalConfig.upload_datas.crop_image" size="small">
                            <el-radio :label=true>是</el-radio>
                            <el-radio :label=false>否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="是否上传全图" prop="full_image">
                        <el-radio-group v-model="globalConfig.upload_datas.full_image" size="small">
                            <el-radio :label=true>是</el-radio>
                            <el-radio :label=false>否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="是否上传分析结果" prop="result">
                        <el-radio-group v-model="globalConfig.upload_datas.result" size="small">
                            <el-radio :label=true>是</el-radio>
                            <el-radio :label=false>否</el-radio>
                        </el-radio-group>

                    </el-form-item>
                </el-form>
                <div style="width: 200px; margin: 0 auto">
                    <el-button :loading="buttonLoading" type="primary" @click="submitGlobalConfigForm">保存</el-button>
                </div>

            </el-card>

        </el-tab-pane>

    </el-tabs>
</template>

<script>
import request from '@/utils/request'

/* {
 * "upload_address": "192.168.1.104:18185",
 * "upload_image_type": 0,
 * "upload_frame_results": false,
 * "upload_type": 0,
 * "storage_dir": "./data",
 * "upload_datas": {
 * "crop_image": true,
 * "full_image": false,
 * "result": true
 *  }
 * }
 */

export default {
    data() {
        return {
            buttonLoading: false,

            activeName: 'first',
            serverStatus: '',
            serviceVersion: '',
            algorithmVersion: '',
            licenseStatus: '',

            globalConfig: {
                upload_address: '/system/analysisResult/event/result',
                storage_dir: '/data/images/',
                upload_type: 0,
                upload_image_type: 1,
                upload_frame_results: false,
                upload_datas: {
                    crop_image: true,
                    full_image: true,
                    result: true
                }
            }
        };
    },
    created() {
        this.getAlgorithm()
        this.getLicense()
    },
    methods: {
        handleClick(tab, event) {
            console.log(tab, event);
        },

        getAlgorithm() {
            this.getAlgorithmRequest().then(response => {
                //console.log(response)
                if (response.data == null) {
                    this.serverStatus = "异常"
                } else {
                    this.serverStatus = "正常"
                    this.serviceVersion = response.data.data.server_version;
                    this.algorithmVersion = response.data.data.algorithm_version
                }
            })
        },

        getLicense() {
            this.getLicenseRequest().then(response => {
                //console.log(response)
                if (response.data != null) {
                    if (response.data.data.server_status == 3) {
                        this.licenseStatus = '正常'
                    } else {
                        this.licenseStatus = '异常'
                    }
                }
            })
        },

        getAlgorithmRequest() {
            return request({
                url: '/system/algorithm/version',
                method: 'get'
            })
        },

        getLicenseRequest() {
            return request({
                url: '/system/algorithm/info',
                method: 'get'
            })
        },

        updateGlobalConfig(form) {
            return request({
                url: '/system/algorithm/updateGlobalConfig',
                method: 'post',
                data: form
            })
        },

        submitGlobalConfigForm() {
            this.$refs["globalConfig"].validate(valid => {
                if (valid) {
                    //console.log(this.form.startTime)
                    this.buttonLoading = true;
                    this.updateGlobalConfig(this.globalConfig).then(response => {
                        this.$modal.msgSuccess("保存成功");
                        this.buttonLoading = false;
                    }).finally(() => {
                        this.buttonLoading = false;
                    });
                }
            });
        },

        submitRestartAlgorithm(){
            this.buttonLoading=true
            this.restartAlgorithm().then(res=>{
                this.buttonLoading=false
                if(res.code==200){
                    this.$modal.msgSuccess("重启成功");
                }else{
                    this.$modal.msgError('重启失败');
                }
            })
        },

        restartAlgorithm(){
            return request({
                url: '/system/algorithm/restartAlgorithm',
                method: 'get'
            })
        }

    }
};

</script>
<style scoped lang="scss">
.box-card {
    width: 600px;
    margin: 0 auto;
}
</style>